#pragma once
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <memory>
#include <vector>
#include <assert.h>
#include "log.hpp"
#include <mutex>
#include <cstdint>
#include <jsoncpp/json/json.h>
#include <mysql/mysql.h>
class mysql_util
{
public:
    static MYSQL *mysql_create(const std::string &host,
                               const std::string &username,
                               const std::string &password,
                               const std::string &dbname,
                               uint16_t port = 3306)
    {
        // 1. 初始化mysql句柄
        MYSQL *mysql = mysql_init(NULL);
        if (mysql == NULL)
        {
            LOG(ERROR, "mysql init failed!\n");
            return nullptr;
        }
        // 2. 连接服务器
        if (mysql_real_connect(mysql, host.c_str(), username.c_str(), password.c_str(), dbname.c_str(), port, NULL, 0) == NULL)
        {
            LOG(ERROR, "connect mysql server failed : %s\n", mysql_error(mysql));
            mysql_close(mysql);
            return nullptr;
        }
        // 3. 设置客户端字符集
        if (mysql_set_character_set(mysql, "utf8") != 0)
        {
            LOG(FATAL, "set client character failed : %s\n", mysql_error(mysql));
            mysql_close(mysql);
            return nullptr;
        }
        return mysql;
    }
    static bool mysql_exec(MYSQL *mysql, const std::string &sql)
    {
        int ret = mysql_query(mysql, sql.c_str());
        if (ret != 0)
        {
            LOG(FATAL, "mysql query failed : %s\n", mysql_error(mysql));
            return false;
        }
        return true;
    }
    static void mysql_destory(MYSQL *mysql)
    {
        if (mysql != NULL)
        {
            mysql_close(mysql);
        }
        return;
    }
};
class json_util
{
public:
    static bool serialize(const Json::Value &root, std::string &str)
    {
        // 2. 实例化一个StreamWriterBilder工厂类对象
        Json::StreamWriterBuilder swb;
        // 3. 通过StreamWriterBilder工厂对象生产一个StreamWriter对象
        std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
        // 4. 通过StreamWriter对象对：Json:Value中存储的数据进行序列化
        std::stringstream ss;
        int ret = sw->write(root, &ss); // 向字符流中写入格式化后的字符串
        if (ret != 0)
        {
            LOG(FATAL, "writer error \n");
            return "";
        }
        str = ss.str();
        return true;
    }
    static bool unserialize(const std::string &str, Json::Value &root)
    {
        // 2. 实例化一个CharReaderBilder工厂类对象
        Json::CharReaderBuilder cb;
        // 3. 通过CharReaderBilder工厂对象生产一个CharStream对象
        std::unique_ptr<Json::CharReader> cr(cb.newCharReader());
        // 3. 定义一个Json::Value对象存储解析后数据
        std::string err;
        // 4. 通过CharStream对象对：Json格式字符串进行反序列化:err:该变量用于存储解析过程中可能出现的错误信息
        bool ret = cr->parse(str.c_str(), str.c_str() + str.size(), &root, &err);
        if (ret == false)
        {
            LOG(FATAL, "parse error!:%s\n", err.c_str());
            return false;
        }
        return true;
    }
};
class string_util
{
public:
    // 分割字符串
    static int split(const std::string &src, const std::string &sep, std::vector<std::string> &res)
    {
        size_t pos = 0;
        size_t index = 0;
        while (index < src.size())
        {
            // 注意从index位置开始查找
            pos = src.find(sep, index);
            // 当没有找到sep时
            if (pos == std::string::npos)
            {
                res.push_back(src.substr(index));
                break;
            }
            // 当index下标位置处为sep起始时
            if (index == pos)
            {
                index += sep.size();
                continue;
            }
            res.push_back(src.substr(index, pos - index));
            index = pos + sep.size();
        }
        return res.size();
    }
};
class file_util
{
public:
    static bool read(const std::string &filename, std::string &body)
    {
        // 打开文件
        std::ifstream ifs(filename, std::ios::binary);
        if (!ifs.is_open())
        {
            LOG(FATAL, "open %s file failed\n", filename);
            return false;
        }
        // 计算文件大小
        size_t filesize = 0;
        ifs.seekg(0, std::ios::end);
        filesize = ifs.tellg();
        ifs.seekg(0, std::ios::beg);
        body.resize(filesize);
        // 将文件中所有数据读取出来
        ifs.read(&body[0], filesize);
        if (ifs.good() == false)
        {
            LOG(FATAL, "open %s file failed\n", filename);
            ifs.close();
            return false;
        }
        ifs.close();
        return true;
    }
};